package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //角色管理分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult =  roleService.findPage(queryPageBean);
      //  System.out.println("返回的分页结果=="+pageResult);
        return  pageResult;
    }


    //添加角色:
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] checkitemIds,Integer[] checkitemIdsMenu) {
        try {
            System.out.println("菜单选项==="+checkitemIdsMenu);
            roleService.add(role,checkitemIds,checkitemIdsMenu);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role =  roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }


    /**
     * 根据roleId查询权限项的ids
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkGroupId){
        try {
            List<Integer> checkItemIds =  roleService.findCheckItemIdsByCheckGroupId(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据roleId查询菜单项的ids
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupIdMenu")
    public Result findCheckItemIdsByCheckGroupIdMenu(Integer checkGroupId){
        try {
            List<Integer> checkItemIds =  roleService.findCheckItemIdsByCheckGroupIdMenu(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //更新
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] checkitemIds,Integer[] checkitemIdsMenu){
        try {
            roleService.edit(role,checkitemIds,checkitemIdsMenu);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //    删除数据:
    @RequestMapping("/delete")
    public Result delete(Integer checkGroupId){
        try {
            roleService.delete(checkGroupId);
            return new Result(true,"删除检查组成功...");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除检查组失败...");

        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> list =  roleService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
